//
//  WelcomeViewController.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/6/28.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let urlString = sharedUserAccount?.avatar_large {

            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 更改底部约束
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            // 调用了这个才会让更改的底部约束生效
            self.view.layoutIfNeeded()
            
            }) { _ in
                // 欢迎文字慢慢显示
                UIView.animateWithDuration(1.0, animations: {
                    self.welcomeLabel.alpha = 1.0
                    
                    
                    }, completion: { _ in
                        print("OK")
                        NSNotificationCenter.defaultCenter().postNotificationName(YQSwitchRootVCNotification, object: true)
                })
        }
    }
    
    var bottomConstraint: NSLayoutConstraint?
    override func loadView() {

        view = UIView()
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        // 需要先添加背景图片再添加其他控件
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
 
        // 设置背景图片的约束，使之铺满
        backImageView.ff_Fill(view)

        // 先记录 iconView 上添加的约束
        let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(90, 90), offset: CGPoint(x: 0, y: -160))
        bottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)

        welcomeLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 8))
    
    }
    // 用户头像
    lazy var iconView: UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.masksToBounds = true
        icon.layer.cornerRadius = 45
        return icon
    }()
    // 欢迎语
    lazy var welcomeLabel: UILabel = {
       let welLabel = UILabel()
        welLabel.text = "欢迎回来"
        welLabel.sizeToFit()
        // 设置透明度为0
        welLabel.alpha = 0
        return welLabel
    }()
}
